home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / usr_-_Usr_Files / BIN / SPLAIN < prev    next >
Text File  |  1999-09-17  |  14KB  |  536 lines

  1. #!/usr/bin/perl
  2.     eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
  3.     if $running_under_some_shell;
  4.  
  5. =head1 NAME
  6.  
  7. diagnostics - Perl compiler pragma to force verbose warning diagnostics
  8.  
  9. splain - standalone program to do the same thing
  10.  
  11. =head1 SYNOPSIS
  12.  
  13. As a pragma:
  14.  
  15.     use diagnostics;
  16.     use diagnostics -verbose;
  17.  
  18.     enable  diagnostics;
  19.     disable diagnostics;
  20.  
  21. Aa a program:
  22.  
  23.     perl program 2>diag.out
  24.     splain [-v] [-p] diag.out
  25.  
  26.  
  27. =head1 DESCRIPTION
  28.  
  29. =head2 The C<diagnostics> Pragma
  30.  
  31. This module extends the terse diagnostics normally emitted by both the
  32. perl compiler and the perl interpreter, augmenting them with the more
  33. explicative and endearing descriptions found in L<perldiag>.  Like the
  34. other pragmata, it affects the compilation phase of your program rather
  35. than merely the execution phase.
  36.  
  37. To use in your program as a pragma, merely invoke
  38.  
  39.     use diagnostics;
  40.  
  41. at the start (or near the start) of your program.  (Note 
  42. that this I<does> enable perl's B<-w> flag.)  Your whole
  43. compilation will then be subject(ed :-) to the enhanced diagnostics.
  44. These still go out B<STDERR>.
  45.  
  46. Due to the interaction between runtime and compiletime issues,
  47. and because it's probably not a very good idea anyway,
  48. you may not use C<no diagnostics> to turn them off at compiletime.
  49. However, you may control there behaviour at runtime using the 
  50. disable() and enable() methods to turn them off and on respectively.
  51.  
  52. The B<-verbose> flag first prints out the L<perldiag> introduction before
  53. any other diagnostics.  The $diagnostics::PRETTY variable can generate nicer
  54. escape sequences for pagers.
  55.  
  56. =head2 The I<splain> Program
  57.  
  58. While apparently a whole nuther program, I<splain> is actually nothing
  59. more than a link to the (executable) F<diagnostics.pm> module, as well as
  60. a link to the F<diagnostics.pod> documentation.  The B<-v> flag is like
  61. the C<use diagnostics -verbose> directive.
  62. The B<-p> flag is like the
  63. $diagnostics::PRETTY variable.  Since you're post-processing with 
  64. I<splain>, there's no sense in being able to enable() or disable() processing.
  65.  
  66. Output from I<splain> is directed to B<STDOUT>, unlike the pragma.
  67.  
  68. =head1 EXAMPLES
  69.  
  70. The following file is certain to trigger a few errors at both
  71. runtime and compiletime:
  72.  
  73.     use diagnostics;
  74.     print NOWHERE "nothing\n";
  75.     print STDERR "\n\tThis message should be unadorned.\n";
  76.     warn "\tThis is a user warning";
  77.     print "\nDIAGNOSTIC TESTER: Please enter a <CR> here: ";
  78.     my $a, $b = scalar <STDIN>;
  79.     print "\n";
  80.     print $x/$y;
  81.  
  82. If you prefer to run your program first and look at its problem
  83. afterwards, do this:
  84.  
  85.     perl -w test.pl 2>test.out
  86.     ./splain < test.out
  87.  
  88. Note that this is not in general possible in shells of more dubious heritage, 
  89. as the theoretical 
  90.  
  91.     (perl -w test.pl >/dev/tty) >& test.out
  92.     ./splain < test.out
  93.  
  94. Because you just moved the existing B<stdout> to somewhere else.
  95.  
  96. If you don't want to modify your source code, but still have on-the-fly
  97. warnings, do this:
  98.  
  99.     exec 3>&1; perl -w test.pl 2>&1 1>&3 3>&- | splain 1>&2 3>&- 
  100.  
  101. Nifty, eh?
  102.  
  103. If you want to control warnings on the fly, do something like this.
  104. Make sure you do the C<use> first, or you won't be able to get
  105. at the enable() or disable() methods.
  106.  
  107.     use diagnostics; # checks entire compilation phase 
  108.     print "\ntime for 1st bogus diags: SQUAWKINGS\n";
  109.     print BOGUS1 'nada';
  110.     print "done with 1st bogus\n";
  111.  
  112.     disable diagnostics; # only turns off runtime warnings
  113.     print "\ntime for 2nd bogus: (squelched)\n";
  114.     print BOGUS2 'nada';
  115.     print "done with 2nd bogus\n";
  116.  
  117.     enable diagnostics; # turns back on runtime warnings
  118.     print "\ntime for 3rd bogus: SQUAWKINGS\n";
  119.     print BOGUS3 'nada';
  120.     print "done with 3rd bogus\n";
  121.  
  122.     disable diagnostics;
  123.     print "\ntime for 4th bogus: (squelched)\n";
  124.     print BOGUS4 'nada';
  125.     print "done with 4th bogus\n";
  126.  
  127. =head1 INTERNALS
  128.  
  129. Diagnostic messages derive from the F<perldiag.pod> file when available at
  130. runtime.  Otherwise, they may be embedded in the file itself when the
  131. splain package is built.   See the F<Makefile> for details.
  132.  
  133. If an extant $SIG{__WARN__} handler is discovered, it will continue
  134. to be honored, but only after the diagnostics::splainthis() function 
  135. (the module's $SIG{__WARN__} interceptor) has had its way with your
  136. warnings.
  137.  
  138. There is a $diagnostics::DEBUG variable you may set if you're desperately
  139. curious what sorts of things are being intercepted.
  140.  
  141.     BEGIN { $diagnostics::DEBUG = 1 } 
  142.  
  143.  
  144. =head1 BUGS
  145.  
  146. Not being able to say "no diagnostics" is annoying, but may not be
  147. insurmountable.
  148.  
  149. The C<-pretty> directive is called too late to affect matters.
  150. You have to do this instead, and I<before> you load the module.
  151.  
  152.     BEGIN { $diagnostics::PRETTY = 1 } 
  153.  
  154. I could start up faster by delaying compilation until it should be
  155. needed, but this gets a "panic: top_level" when using the pragma form
  156. in Perl 5.001e.
  157.  
  158. While it's true that this documentation is somewhat subserious, if you use
  159. a program named I<splain>, you should expect a bit of whimsy.
  160.  
  161. =head1 AUTHOR
  162.  
  163. Tom Christiansen <F<tchrist@mox.perl.com>>, 25 June 1995.
  164.  
  165. =cut
  166.  
  167. require 5.001;
  168. use Carp;
  169.  
  170. use Config;
  171. ($privlib, $archlib) = @Config{qw(privlibexp archlibexp)};
  172. if ($^O eq 'VMS') {
  173.     require VMS::Filespec;
  174.     $privlib = VMS::Filespec::unixify($privlib);
  175.     $archlib = VMS::Filespec::unixify($archlib);
  176. }
  177. @trypod = ("$archlib/pod/perldiag.pod",
  178.        "$privlib/pod/perldiag-$].pod",
  179.        "$privlib/pod/perldiag.pod");
  180. # handy for development testing of new warnings etc
  181. unshift @trypod, "./pod/perldiag.pod" if -e "pod/perldiag.pod";
  182. ($PODFILE) = ((grep { -e } @trypod), $trypod[$#trypod])[0];
  183.  
  184. $DEBUG ||= 0;
  185. my $WHOAMI = ref bless [];  # nobody's business, prolly not even mine
  186.  
  187. $| = 1;
  188.  
  189. local $_;
  190.  
  191. CONFIG: {
  192.     $opt_p = $opt_d = $opt_v = $opt_f = '';
  193.     %HTML_2_Troff = %HTML_2_Latin_1 = %HTML_2_ASCII_7 = ();  
  194.     %exact_duplicate = ();
  195.  
  196.     unless (caller) { 
  197.     $standalone++;
  198.     require Getopt::Std;
  199.     Getopt::Std::getopts('pdvf:')
  200.         or die "Usage: $0 [-v] [-p] [-f splainpod]";
  201.     $PODFILE = $opt_f if $opt_f;
  202.     $DEBUG = 2 if $opt_d;
  203.     $VERBOSE = $opt_v;
  204.     $PRETTY = $opt_p;
  205.     } 
  206.  
  207.     if (open(POD_DIAG, $PODFILE)) {
  208.     warn "Happy happy podfile from real $PODFILE\n" if $DEBUG;
  209.     last CONFIG;
  210.     } 
  211.  
  212.     if (caller) {
  213.     INCPATH: {
  214.         for $file ( (map { "$_/$WHOAMI.pm" } @INC), $0) {
  215.         warn "Checking $file\n" if $DEBUG;
  216.         if (open(POD_DIAG, $file)) {
  217.             while (<POD_DIAG>) {
  218.             next unless /^__END__\s*# wish diag dbase were more accessible/;
  219.             print STDERR "podfile is $file\n" if $DEBUG;
  220.             last INCPATH;
  221.             }
  222.         }
  223.         } 
  224.     }
  225.     } else { 
  226.     print STDERR "podfile is <DATA>\n" if $DEBUG;
  227.     *POD_DIAG = *main::DATA;
  228.     }
  229. }
  230. if (eof(POD_DIAG)) { 
  231.     die "couldn't find diagnostic data in $PODFILE @INC $0";
  232. }
  233.  
  234.  
  235. %HTML_2_Troff = (
  236.     'amp'    =>    '&',    #   ampersand
  237.     'lt'    =>    '<',    #   left chevron, less-than
  238.     'gt'    =>    '>',    #   right chevron, greater-than
  239.     'quot'    =>    '"',    #   double quote
  240.  
  241.     "Aacute"    =>    "A\\*'",    #   capital A, acute accent
  242.     # etc
  243.  
  244. );
  245.  
  246. %HTML_2_Latin_1 = (
  247.     'amp'    =>    '&',    #   ampersand
  248.     'lt'    =>    '<',    #   left chevron, less-than
  249.     'gt'    =>    '>',    #   right chevron, greater-than
  250.     'quot'    =>    '"',    #   double quote
  251.  
  252.     "Aacute"    =>    "\xC1"    #   capital A, acute accent
  253.  
  254.     # etc
  255. );
  256.  
  257. %HTML_2_ASCII_7 = (
  258.     'amp'    =>    '&',    #   ampersand
  259.     'lt'    =>    '<',    #   left chevron, less-than
  260.     'gt'    =>    '>',    #   right chevron, greater-than
  261.     'quot'    =>    '"',    #   double quote
  262.  
  263.     "Aacute"    =>    "A"    #   capital A, acute accent
  264.     # etc
  265. );
  266.  
  267. *HTML_Escapes = do {
  268.     if ($standalone) {
  269.     $PRETTY ? \%HTML_2_Latin_1 : \%HTML_2_ASCII_7; 
  270.     } else {
  271.     \%HTML_2_Latin_1; 
  272.     }
  273. }; 
  274.  
  275. *THITHER = $standalone ? *STDOUT : *STDERR;
  276.  
  277. $transmo = <<EOFUNC;
  278. sub transmo {
  279.     local \$^W = 0;  # recursive warnings we do NOT need!
  280.     study;
  281. EOFUNC
  282.  
  283. ### sub finish_compilation {  # 5.001e panic: top_level for embedded version
  284.     print STDERR "FINISHING COMPILATION for $_\n" if $DEBUG;
  285.     ### local 
  286.     $RS = '';
  287.     local $_;
  288.     while (<POD_DIAG>) {
  289.     #s/(.*)\n//;
  290.     #$header = $1;
  291.  
  292.     unescape();
  293.     if ($PRETTY) {
  294.         sub noop   { return $_[0] }  # spensive for a noop
  295.         sub bold   { my $str =$_[0];  $str =~ s/(.)/$1\b$1/g; return $str; } 
  296.         sub italic { my $str = $_[0]; $str =~ s/(.)/_\b$1/g;  return $str; } 
  297.         s/[BC]<(.*?)>/bold($1)/ges;
  298.         s/[LIF]<(.*?)>/italic($1)/ges;
  299.     } else {
  300.         s/[BC]<(.*?)>/$1/gs;
  301.         s/[LIF]<(.*?)>/$1/gs;
  302.     } 
  303.     unless (/^=/) {
  304.         if (defined $header) { 
  305.         if ( $header eq 'DESCRIPTION' && 
  306.             (   /Optional warnings are enabled/ 
  307.              || /Some of these messages are generic./
  308.             ) )
  309.         {
  310.             next;
  311.         } 
  312.         s/^/    /gm;
  313.         $msg{$header} .= $_;
  314.         }
  315.         next;
  316.     } 
  317.     unless ( s/=item (.*)\s*\Z//) {
  318.  
  319.         if ( s/=head1\sDESCRIPTION//) {
  320.         $msg{$header = 'DESCRIPTION'} = '';
  321.         }
  322.         next;
  323.     }
  324.  
  325.     # strip formatting directives in =item line
  326.     ($header = $1) =~ s/[A-Z]<(.*?)>/$1/g;
  327.  
  328.     if ($header =~ /%[sd]/) {
  329.         $rhs = $lhs = $header;
  330.         #if ($lhs =~ s/(.*?)%d(?!%d)(.*)/\Q$1\E\\d+\Q$2\E\$/g)  {
  331.         if ($lhs =~ s/(.*?)%d(?!%d)(.*)/\Q$1\E\\d+\Q$2\E/g)  {
  332.         $lhs =~ s/\\%s/.*?/g;
  333.         } else {
  334.         # if i had lookbehind negations, i wouldn't have to do this \377 noise
  335.         $lhs =~ s/(.*?)%s/\Q$1\E.*?\377/g;
  336.         #$lhs =~ s/\377([^\377]*)$/\Q$1\E\$/;
  337.         $lhs =~ s/\377([^\377]*)$/\Q$1\E/;
  338.         $lhs =~ s/\377//g;
  339.         $lhs =~ s/\.\*\?$/.*/; # Allow %s at the end to eat it all
  340.         } 
  341.         $transmo .= "    s{^$lhs}\n     {\Q$rhs\E}s\n\t&& return 1;\n";
  342.     } else {
  343.         $transmo .= "    m{^\Q$header\E} && return 1;\n";
  344.     } 
  345.  
  346.     print STDERR "$WHOAMI: Duplicate entry: \"$header\"\n"
  347.         if $msg{$header};
  348.  
  349.     $msg{$header} = '';
  350.     } 
  351.  
  352.  
  353.     close POD_DIAG unless *main::DATA eq *POD_DIAG;
  354.  
  355.     die "No diagnostics?" unless %msg;
  356.  
  357.     $transmo .= "    return 0;\n}\n";
  358.     print STDERR $transmo if $DEBUG;
  359.     eval $transmo;
  360.     die $@ if $@;
  361.     $RS = "\n";
  362. ### }
  363.  
  364. if ($standalone) {
  365.     if (!@ARGV and -t STDIN) { print STDERR "$0: Reading from STDIN\n" } 
  366.     while (defined ($error = <>)) {
  367.     splainthis($error) || print THITHER $error;
  368.     } 
  369.     exit;
  370. } else { 
  371.     $old_w = 0; $oldwarn = ''; $olddie = '';
  372. }
  373.  
  374. sub import {
  375.     shift;
  376.     $old_w = $^W;
  377.     $^W = 1; # yup, clobbered the global variable; tough, if you
  378.          # want diags, you want diags.
  379.     return if $SIG{__WARN__} eq \&warn_trap;
  380.  
  381.     for (@_) {
  382.  
  383.     /^-d(ebug)?$/            && do {
  384.                     $DEBUG++;
  385.                     next;
  386.                    };
  387.  
  388.     /^-v(erbose)?$/     && do {
  389.                     $VERBOSE++;
  390.                     next;
  391.                    };
  392.  
  393.     /^-p(retty)?$/         && do {
  394.                     print STDERR "$0: I'm afraid it's too late for prettiness.\n";
  395.                     $PRETTY++;
  396.                     next;
  397.                    };
  398.  
  399.     warn "Unknown flag: $_";
  400.     } 
  401.  
  402.     $oldwarn = $SIG{__WARN__};
  403.     $olddie = $SIG{__DIE__};
  404.     $SIG{__WARN__} = \&warn_trap;
  405.     $SIG{__DIE__} = \&death_trap;
  406.  
  407. sub enable { &import }
  408.  
  409. sub disable {
  410.     shift;
  411.     $^W = $old_w;
  412.     return unless $SIG{__WARN__} eq \&warn_trap;
  413.     $SIG{__WARN__} = $oldwarn;
  414.     $SIG{__DIE__} = $olddie;
  415.  
  416. sub warn_trap {
  417.     my $warning = $_[0];
  418.     if (caller eq $WHOAMI or !splainthis($warning)) {
  419.     print STDERR $warning;
  420.     } 
  421.     &$oldwarn if defined $oldwarn and $oldwarn and $oldwarn ne \&warn_trap;
  422. };
  423.  
  424. sub death_trap {
  425.     my $exception = $_[0];
  426.  
  427.     # See if we are coming from anywhere within an eval. If so we don't
  428.     # want to explain the exception because it's going to get caught.
  429.     my $in_eval = 0;
  430.     my $i = 0;
  431.     while (1) {
  432.       my $caller = (caller($i++))[3] or last;
  433.       if ($caller eq '(eval)') {
  434.     $in_eval = 1;
  435.     last;
  436.       }
  437.     }
  438.  
  439.     splainthis($exception) unless $in_eval;
  440.     if (caller eq $WHOAMI) { print STDERR "INTERNAL EXCEPTION: $exception"; } 
  441.     &$olddie if defined $olddie and $olddie and $olddie ne \&death_trap;
  442.  
  443.     # We don't want to unset these if we're coming from an eval because
  444.     # then we've turned off diagnostics. (Actually what does this next
  445.     # line do?  -PSeibel)
  446.     $SIG{__DIE__} = $SIG{__WARN__} = '' unless $in_eval;
  447.     local($Carp::CarpLevel) = 1;
  448.     confess "Uncaught exception from user code:\n\t$exception";
  449.     # up we go; where we stop, nobody knows, but i think we die now
  450.     # but i'm deeply afraid of the &$olddie guy reraising and us getting
  451.     # into an indirect recursion loop
  452. };
  453.  
  454. sub splainthis {
  455.     local $_ = shift;
  456.     local $\;
  457.     ### &finish_compilation unless %msg;
  458.     s/\.?\n+$//;
  459.     my $orig = $_;
  460.     # return unless defined;
  461.     if ($exact_duplicate{$_}++) {
  462.     return 1;
  463.     } 
  464.     s/, <.*?> (?:line|chunk).*$//;
  465.     $real = s/(.*?) at .*? (?:line|chunk) \d+.*/$1/;
  466.     s/^\((.*)\)$/$1/;
  467.     return 0 unless &transmo;
  468.     $orig = shorten($orig);
  469.     if ($old_diag{$_}) {
  470.     autodescribe();
  471.     print THITHER "$orig (#$old_diag{$_})\n";
  472.     $wantspace = 1;
  473.     } else {
  474.     autodescribe();
  475.     $old_diag{$_} = ++$count;
  476.     print THITHER "\n" if $wantspace;
  477.     $wantspace = 0;
  478.     print THITHER "$orig (#$old_diag{$_})\n";
  479.     if ($msg{$_}) {
  480.         print THITHER $msg{$_};
  481.     } else {
  482.         if (0 and $standalone) { 
  483.         print THITHER "    **** Error #$old_diag{$_} ",
  484.             ($real ? "is" : "appears to be"),
  485.             " an unknown diagnostic message.\n\n";
  486.         }
  487.         return 0;
  488.     } 
  489.     }
  490.     return 1;
  491.  
  492. sub autodescribe {
  493.     if ($VERBOSE and not $count) {
  494.     print THITHER &{$PRETTY ? \&bold : \&noop}("DESCRIPTION OF DIAGNOSTICS"),
  495.         "\n$msg{DESCRIPTION}\n";
  496.     } 
  497.  
  498. sub unescape { 
  499.     s {
  500.             E<  
  501.             ( [A-Za-z]+ )       
  502.             >   
  503.     } { 
  504.          do {   
  505.              exists $HTML_Escapes{$1}
  506.                 ? do { $HTML_Escapes{$1} }
  507.                 : do {
  508.                     warn "Unknown escape: E<$1> in $_";
  509.                     "E<$1>";
  510.                 } 
  511.          } 
  512.     }egx;
  513. }
  514.  
  515. sub shorten {
  516.     my $line = $_[0];
  517.     if (length($line) > 79 and index($line, "\n") == -1) {
  518.     my $space_place = rindex($line, ' ', 79);
  519.     if ($space_place != -1) {
  520.         substr($line, $space_place, 1) = "\n\t";
  521.     } 
  522.     } 
  523.     return $line;
  524.  
  525.  
  526. # have to do this: RS isn't set until run time, but we're executing at compile time
  527. $RS = "\n";
  528.  
  529. 1 unless $standalone;  # or it'll complain about itself
  530. __END__ # wish diag dbase were more accessible
  531.